<template>
  <div class="rankings-wrapper">
    <el-card class="block-card">
      <div class="page-header">
        <h2 class="page-title">排名</h2>
      </div>
      <div class="rankings-controls">
        <div class="right-controls">
          <el-input v-model="search" size="small" placeholder="名字" style="width: 240px; margin-right: 12px;" clearable />
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">查找</el-button>
        </div>
      </div>
      <el-table :data="pagedData" class="custom-table" style="width: 100%; margin-top: 24px;" :show-header="true" :border="false" header-row-class-name="custom-header-row" row-class-name="custom-row">
        <el-table-column label="榜单" align="left" header-align="left">
          <template slot-scope="scope">
            {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="student_id" label="用户" align="left" header-align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.student_id }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="username" label="名字" align="left" header-align="left" />
        <el-table-column prop="solved" label="解决题目/提交次数" align="left" header-align="left">
          <template slot-scope="scope">
            {{ scope.row.passed_count }} / {{ scope.row.submission_count }}
          </template>
        </el-table-column>
        <el-table-column prop="rate" label="题目解决率" align="left" header-align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.pass_rate }}%</span>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <PageCom 
        :total="total" 
        :pageSize="pageSize"
        :currentPage="currentPage"
        @handleCurrentChange="handlePageChange"
        @handleSizeChange="handlePageSizeChange"
      />
    </el-card>
  </div>
</template>

<script>
import { rankList } from '@/api/oj'
import PageCom from '@/components/PageCom.vue'
export default {
  name: 'RankingsView',
  components: { PageCom },
  data() {
    return {
      rankings: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      search: '',
      loading: false
    }
  },
  mounted() {
    this.fetchRankings()
  },
  computed: {
    pagedData() {
      return this.rankings
    }
  },
  methods: {
    async fetchRankings() {
      this.loading = true
      try {
        const params = {
          page: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.search
        }
        const response = await rankList(params)
        if (response.data.code === 200) {
          this.rankings = response.data.data.data
          this.total = response.data.data.total
        } else {
          this.$message.error(response.data.message || '获取排行榜失败')
        }
      } catch (error) {
        this.$message.error('获取排行榜失败')
      } finally {
        this.loading = false
      }
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchRankings()
    },
    handlePageSizeChange(size) {
      this.pageSize = size
      this.currentPage = 1
      this.fetchRankings()
    },
    handleSearch() {
      this.currentPage = 1
      this.fetchRankings()
    }
  }
}
</script>

<style scoped>
.rankings-wrapper {
  padding: 10px 0 24px 0;
  box-sizing: border-box;
}
.block-card {
  margin-bottom: 24px;
  width: 100%;
  /* el-card 默认圆角和阴影 */
}
.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}
.page-title {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  color: #222;
}
.rankings-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.left-controls {
  flex: 1;
}
.right-controls {
  display: flex;
  align-items: center;
  gap: 0 8px;
}
.custom-pagination {
  margin-bottom: 0;
}
.custom-table {
  background: #fff;
  font-size: 15px;
}
.custom-header-row th {
  font-weight: bold !important;
  font-size: 16px !important;
  color: #222 !important;
  background: #fff !important;
  border-bottom: 1.5px solid #f0f0f0 !important;
}
.custom-row td {
  color: #555;
  background: #fff;
  border-bottom: 1px solid #f5f5f5 !important;
  text-align: left !important;
}
.user-link {
  color: #13c2c2;
  text-decoration: underline;
  font-weight: 500;
}
.el-table th, .el-table td {
  padding: 10px 0 10px 8px !important;
}
</style>
